#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>

int main(void)
{   
    int n=5;
	for(int i = 0; i < n; i++)
    {
        pid_t pid = fork();
        if(pid < 0){
            perror("fork err");
            return -1;
        }else if(pid > 0)
        {   
            while(1){
                int ret=waitpid(pid,0,WNOHANG);
                if(ret > 0){
                    break;
                }else if(ret < 0){
                    printf("waitpid err");
                }
                sleep(1);
            }
            printf("in parent (PID:%d),child:%d\n",getpid(),pid);
            exit(0);
        }
    }

    printf("in child (PID:%d), parent:%d\n",getpid(),getppid());

    
	return 0;
}
